/* Includes ------------------------------------------------------------------*/
#include "gt32f030.h"
#include "gt32f030_uart.h"
#include "gt32f030_rcc.h"
#include "system_gt32f030.h"


/**
  * @brief  Deinitializes the UARTx peripheral registers to their default reset values.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *         This parameter can be one of the following values: UART0, UART1.      
  * @retval None
  */
void UART_DeInit(UART_TypeDef* UARTx)
{
	/* Check the parameters */
	assert_param(IS_UART_ALL_PERIPH(UARTx));

	if (UARTx == UART0)
	{
		/* Enable UART0 reset state */
		RCC_PeriphResetCmd(RCC_PeriphReset_UART0, ENABLE);
		/* Release UART0 from reset state */
		RCC_PeriphResetCmd(RCC_PeriphReset_UART0, DISABLE);
	}
	else if (UARTx == UART1)
	{		
		/* Enable UART1 reset state */
		RCC_PeriphResetCmd(RCC_PeriphReset_UART1, ENABLE);
		/* Release UART1 from reset state */
		RCC_PeriphResetCmd(RCC_PeriphReset_UART1, DISABLE);
	}
//	else 
//	{ 
//		; /*!< No more uarts*/
//	}
}

/**
  * @brief  Initializes the UARTx peripheral according to the specified
  *         parameters in the UART_InitStruct .
  * @param  UARTx: Select the UART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0, UART1.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure
  *         that contains the configuration information for the specified UART 
  *         peripheral.
  * @retval None
  */


void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct)
{
	/* Check the parameters */
	float baudrate = SystemCoreClock*1.0f;
	assert_param(IS_UART_ALL_PERIPH(UARTx));
	assert_param(IS_UART_BAUDRATE(UART_InitStruct->UART_BaudRate));  
	assert_param(IS_UART_MODE(UART_InitStruct->UART_Mode));

	/*Initialize BIT6 and BIT7*/
	UARTx->CR &= ~(UART_CR_SM1 | UART_CR_SM0);
	UARTx->CR &= ~UART_CR_DBAUD;

	/*Configure UART_Mode0 and UART_Mode2 with fixed baud rate*/
	if ((UART_InitStruct->UART_Mode == UART_Mode_0) || (UART_InitStruct->UART_Mode == UART_Mode_2))
	{
		UARTx->CR |= (UART_InitStruct->UART_Mode << 6);
		UARTx->CR |= (UART_InitStruct->UART_BaudRateDouble << 9);
	} 
	else 
	{
		UARTx->CR |= (UART_InitStruct->UART_Mode << 6);
		UARTx->CR |= (UART_InitStruct->UART_BaudRateDouble << 9);

		baudrate /=(UART_InitStruct->UART_BaudRate*32);
		baudrate *=(UART_InitStruct->UART_BaudRateDouble+1);
		UARTx->BAUD = (unsigned short int)(baudrate - 0.5);
		UARTx->BAUD |= UART_SELF_BRG;
  }		
}

/**
  * @brief  Fills each UART_InitStruct member with its default value.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void UART_StructInit(UART_InitTypeDef* UART_InitStruct)
{
	/* UART_InitStruct members default value */
	UART_InitStruct->UART_BaudRate = 9600;
	UART_InitStruct->UART_Mode = UART_Mode_0;
	UART_InitStruct->UART_BaudRateDouble = 0; 
	//UART_InitStruct->UART_BaudRateTimer_Selected = 0x00;
}

/**
  * @brief  Enables or disables the specified UART peripheral.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *         This parameter can be one of the following values:
  *           UART0, UART1.
  * @param  FuncState: Select the UART Transmit or Recvied function.
  *         This parameter can be one of the following values:
  *           UART_RXEN_EABLE
  *           UART_TXEN_EABLE
  * @param  NewState: new state of the UARTx peripheral.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None  
  */
void UART_FrameModeConfig(UART_TypeDef* UARTx, uint32_t FrameMode)
{
	/* Check the parameters */
	assert_param(IS_USART_ALL_PERIPH(UARTx));
	assert_param(IS_UART_FRAMEMODE(NewState));
	
	UARTx->CR &= ~UART_CR_REN;
	UARTx->CR |= FrameMode;
}

/**
  * @brief  Enables or disables the specified UART interrupts.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0, UART1, 
  * @param  UART_IT: specifies the UART interrupt sources to be enabled or disabled.
  *   This parameter can be one of the following values:
  *     @arg UART_RIEN_EABLE:  Enable UART Recvied interrupt 
  *     @arg UART_TIEN_EABLE:  Enable UART Transmit interrupt 
  * @param  NewState: new state of the specified UARTx interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void UART_ITConfig(UART_TypeDef* UARTx, uint32_t UART_IT, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_UART_ALL_PERIPH(UARTx));
    assert_param(IS_UART_IT(UART_IT));

    if (NewState != DISABLE)
	{
        /*Interrupt enable*/
        UARTx->CR |= UART_IT;
	}
	else
	{
		/*Interrupt disable*/
		UARTx->CR &= ~UART_IT;	
	}
}



/**
  * @brief  Transmits single data through the UARTx peripheral.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0, UART1.
  * @param  Data: the data to transmit.
  * @retval None
  */
void UART_SendData(UART_TypeDef* UARTx, unsigned char Data)
{
	/* Check the parameters */
	assert_param(IS_UART_ALL_PERIPH(UARTx));

	/* Transmit Data */
	UARTx->SBUF = Data;
}

/**
  * @brief  Returns the most recent received data by the UARTx peripheral.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0, UART1.
  * @retval The received data.
  */
unsigned char UART_ReceiveData(UART_TypeDef* UARTx)
{
	/* Check the parameters */
	assert_param(IS_UART_ALL_PERIPH(UARTx));

	/* Receive Data */
	return (unsigned char)(UARTx->SBUF & 0xFF);
}





/**
  * @brief  Checks whether the specified UART interrupt has occurred or not.
  * @param  UARTx: Select the UART or the UART peripheral. 
  *   This parameter can be one of the following values:
  *   UART0, UART1.
  * @param  UART_IT: specifies the UART interrupt source to check.
  *   This parameter can be one of the following values:
  *     @arg UART_ISR_RI:  Recive Data interrupution
  *     @arg UART_ISR_TI:  Tansmit Data Register empty interruption
  *     @arg UART_ISR_FEI: Receiving frame error interruption
  *	    @arg UART_ISR_TxBuffer_Empty: Tansmit buffer empty interruption
  * @retval The new state of UART_IT (SET or RESET).
  */
ITStatus UART_GetITStatus(UART_TypeDef* UARTx, uint32_t UART_IT)
{
	FlagStatus bitstatus = RESET;
	/* Check the parameters */
	assert_param(IS_UART_ALL_PERIPH(UARTx));

	if ((UARTx->INTSR & UART_IT) != (uint32_t)RESET)
	{
		bitstatus = SET;
	}
	else
	{
		bitstatus = RESET;
	}
	return bitstatus;
}

/**
  * @brief  Clears the UARTx's interrupt pending bits.
  * @param  UARTx: Select the UART or the UART peripheral.
  *   This parameter can be one of the following values:
  *   UART0, UART1.
  * @param  UART_IT: specifies the UART interrupt source to check.
  *   This parameter can be one of the following values:
  *     @arg UART_ISR_RI:  Recive Data interrupution
  *     @arg UART_ISR_TI:  Tansmit Data Register empty interruption
  *     @arg UART_ISR_FEI: Receiving frame error interruption
  * @retval None
  */
void UART_ClearIT(UART_TypeDef* UARTx, uint32_t UART_IT)
{
	/* Check the parameters */
	assert_param(IS_UART_ALL_PERIPH(UARTx));
	/*Clear interrupt pending bit*/
	UARTx->INTCLR = UART_IT;
}

